package com.songlanyun.modules.comment.controller.api;

import com.songlanyun.common.annotation.RepeatSubmit;
import com.songlanyun.common.enums.CommentConstant;
import com.songlanyun.common.exception.RRException;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.R;
import com.songlanyun.common.validator.ValidatorUtils;
import com.songlanyun.modules.account.controller.common.AbstractApiController;
import com.songlanyun.modules.comment.entity.OrderGoodsComment;
import com.songlanyun.modules.comment.model.dto.OrderGoodsCommentDTO;
import com.songlanyun.modules.comment.service.OrderGoodsCommentService;
import com.songlanyun.modules.exception.MemberException;
import com.songlanyun.modules.order.service.ChildOrderService;
import com.songlanyun.modules.sensitive.service.SystemSensitiveWordService;
import com.songlanyun.pay.configure.WxPayConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 订单商品评论
 *
 * @author Mark
 * @email sunlightcs@gmail.com
 * @date 2020-11-30 18:09:47
 */
@RestController
@RequestMapping("api/comment/orderGoods")
public class OrderGoodsCommentApiController extends AbstractApiController {
    @Autowired
    private OrderGoodsCommentService orderGoodsCommentService;

    @Resource
    private SystemSensitiveWordService systemSensitiveWordService;
    @Resource
    private ChildOrderService childOrderService;

    /**
     * 会员查询评论列表与商家回复
     */
    @GetMapping("/getComments")
//    @RequiresPermissions("comment:orderGoodsComment:list")
    public R getComments(@RequestParam Map<String, Object> params) {
        params.put("user_id", getUserId(true));
        List<OrderGoodsComment> list = orderGoodsCommentService.getMemberComments(params);

        return R.ok().put("list", list);
    }

    /**
     * 会员查询商品的所有评论与商家回复
     */
    @GetMapping("/getgoodscomments/{goodsId}")
//    @RequiresPermissions("comment:orderGoodsComment:list")
    public R getgoodscomments(@PathVariable("goodsId") Long goodsId) {
        Map<String, Object> params = new HashMap<>();
        params.put("goods_id", goodsId);
        List<OrderGoodsComment> list = orderGoodsCommentService.getMemberComments(params);

        return R.ok().put("list", list);
    }

    /**
     * 会员查询订单的所有评论与商家回复
     */
    @GetMapping("/getordercomments/{orderId}")
//    @RequiresPermissions("comment:orderGoodsComment:list")
    public R getordercomments(@PathVariable("orderId") Long orderId) {
        Map<String, Object> params = new HashMap<>();
        params.put("order_id", orderId);
        List<OrderGoodsComment> list = orderGoodsCommentService.getMemberComments(params);

        return R.ok().put("list", list);
    }

    /**
     * 会员查询服务商品的所有评论与商家回复
     */
    @GetMapping("/service/goods/comments/{goodsId}")
//    @RequiresPermissions("comment:orderGoodsComment:list")
    public R serviceGoodsComments(@PathVariable("goodsId") Long goodsId) {
        Map<String, Object> params = new HashMap<>();
        params.put("goods_id", goodsId);
        params.put("type", CommentConstant.CommentType.SERVICE.getCode());
        List<OrderGoodsComment> list = orderGoodsCommentService.getMemberComments(params);

        return R.ok().put("list", list);
    }

    /**
     * 会员查询服务订单的所有评论与商家回复
     */
    @GetMapping("/service/order/comments/{orderId}")
//    @RequiresPermissions("comment:orderGoodsComment:list")
    public R serviceOrderComments(@PathVariable("orderId") Long orderId) {
        Map<String, Object> params = new HashMap<>();
        params.put("order_id", orderId);
        params.put("type", CommentConstant.CommentType.SERVICE.getCode());
        List<OrderGoodsComment> list = orderGoodsCommentService.getMemberComments(params);

        return R.ok().put("list", list);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
//    @RequiresPermissions("comment:orderGoodsComment:info")
    public R info(@PathVariable("id") Long id) {
        OrderGoodsComment orderGoodsComment = orderGoodsCommentService.getApiById(id);

        return R.ok().put("data", orderGoodsComment);
    }

    /**
     * 会员评论普通商品、订单
     */
    @RepeatSubmit("2")
    @PostMapping("/savegeneralgoods")
//    @RequiresPermissions("comment:orderGoodsComment:save")
    public R savegeneralgoods(@RequestBody OrderGoodsCommentDTO dto) {
        dto.setType(CommentConstant.CommentType.GENERAL.getCode());
        saveComment(dto);
        return R.ok();
    }

    /**
     * 会员评论服务商品、订单
     */
    @PostMapping("/saveservicegoods")
//    @RequiresPermissions("comment:orderGoodsComment:save")
    public R saveservicegoods(@RequestBody OrderGoodsCommentDTO dto) {
        dto.setType(CommentConstant.CommentType.SERVICE.getCode());
        saveComment(dto);
        return R.ok();
    }

    /**
     * 会员评论联盟商家订单
     */
    @RepeatSubmit("2")
    @PostMapping("/saveunionbusiness")
//    @RequiresPermissions("comment:orderGoodsComment:save")
    public R commentUnionBusiness(@RequestBody OrderGoodsCommentDTO dto) {
        ValidatorUtils.validateEntity(dto);
        //过滤敏感词
        dto.setContent(systemSensitiveWordService.filter(dto.getContent()));
        dto.setUserId(getUserId(true));
        orderGoodsCommentService.commentUnionBusiness(dto);
        return R.ok();
    }

    private void saveComment(OrderGoodsCommentDTO dto) {
        ValidatorUtils.validateEntity(dto);
        //过滤敏感词
        String filter = systemSensitiveWordService.filter(dto.getContent());
        dto.setContent(filter);
        dto.setUserId(getUserId(true));

        childOrderService.commentChildOrder(dto);
    }


    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
//    @RequiresPermissions("comment:orderGoodsComment:delete")
    public R delete(@PathVariable("id") Long id) {

        OrderGoodsComment comment = orderGoodsCommentService.getById(id, true);
        verifyMember(comment.getUserId());//校验会员
        orderGoodsCommentService.removeById(id);

        return R.ok();
    }

    /**
     * 校验会员
     *
     * @param userId
     */
    private void verifyMember(Long userId) {
        if (!userId.equals(getUserId(true))) {
            throw new RRException(MemberException.USER_EXCEPTION);
        }
    }


}
